/** @format */

import * as TBAPI from './TBAPI'
import * as JDAPI from './JDAPI'
import { ChangeCreditsinfo } from './interface'

// 由于平台接口参数不同，在此处做处理，调用处无需区分
const CommonPlatApi = {
  /**
   * 会员信息
   * @param context
   * @param session tbRequired
   * @returns
   */
  queryVipinfo: async (context: any, session?: string) => {
    if (context.platform === 'jd') {
      const result = await JDAPI.queryVipinfo(context)
      return {
        code: result?.code,
        result: {
          member_info: result?.data
        }
      }
    }
    return await TBAPI.queryVipinfo(context, session)
  },
  /**
   * 查询积分
   * @param context
   * @param session tbRequired
   * @returns
   */
  queryCredits: async (context: any, session?: string) => {
    if (context.platform === 'jd') {
      const result = await JDAPI.queryCredits(context)
      return result?.data || 0
    }
    return await TBAPI.queryCredits(context, session)
  },
  /**
   * 发放/消耗积分
   * @param context
   * @param options businessId(jdRequired), change_type(tbRequired)
   * @param session tbRequired
   * @returns
   */
  changeCredits: async (context: any, options: ChangeCreditsinfo, session?: string) => {
    if (context.platform === 'jd') {
      const result = await JDAPI.changeCredits(context, options)
      return {
        record_id: result?.data,
        result_msg: result?.desc
      }
    }
    return await TBAPI.changeCredits(context, options, session)
  },
  /**
   * 发放权益
   * @param context
   * @param args jdRequired: couponId, unique_id
   * @returns
   */
  benefitSend: async (context: any, args: any) => {
    if (!args) {
      throw Error('参数args不能为空')
    }
    if (context.platform === 'jd') {
      const { couponId, unique_id } = args
      const result = await JDAPI.benefitSend(context, {
        couponId,
        uuid: unique_id
      })
      return {
        result_success: result?.success,
        result_code: result?.code,
        result_msg: result?.desc?.replace('com.jd.pop.market.api.MarketException:', '').substr(0, 20)
      }
    }
    return await TBAPI.benefitSend(context, args)
  }
}

// 各平台所有接口方法
export const PlatApiFn = {
  ...TBAPI,
  ...JDAPI,
  ...CommonPlatApi
}

// 前置处理请求
const handleRequest = (fn: Function, fnName: string) => {
  return async function (context: any, ...params: any) {
    if (!context) {
      throw Error('参数context不能为空')
    }

    // 京东接口请求
    if (context?.platform === 'jd') {
      const { openId, shopId, appKey, appSecret, venderId } = context
      if (!openId || !shopId || !appKey || !appSecret || !venderId) {
        throw Error('参数context不完整')
      }
      // @ts-ignore
      return JDAPI[fnName] ? fn(context, ...params) : { success: false, msg: `京东无【${fnName}】接口` }
    }

    // 淘宝接口请求
    // @ts-ignore
    return TBAPI[fnName] ? fn(context, ...params) : { success: false, msg: `淘宝无【${fnName}】接口` }
  }
}

// 生成所有平台接口
const genPlatApi = () => {
  const apiList = { ...PlatApiFn }
  for (const fn in apiList) {
    // @ts-ignore
    apiList[fn] = handleRequest(apiList[fn], fn)
  }
  return apiList
}

// 所有平台接口
export const PlatApi = genPlatApi()
